Imports System.Globalization
Partial Class UserControls_DateChooser
    Inherits System.Web.UI.UserControl


    Protected Sub cstmvdtxtDate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cstmvdtxtDate.ServerValidate
        Dim Day As Integer
        Dim Month As Integer
        Dim Year As Integer
        Dim IsLeap As Boolean
        Dim RightNow As Date
        Dim DateArray As Array
        Dim culture As CultureInfo = New CultureInfo("hi-IN", True)

        rgvdtxtDate.Validate()

        If Not rgvdtxtDate.IsValid Then
            Exit Sub
        End If

        RightNow = Now.AddHours(11)
        RightNow = RightNow.AddMinutes(30)

        DateArray = txtDate.Text.Split("/")

        'Code to check the validity of the date


        Day = DateArray(0)
        Month = DateArray(1)
        Year = DateArray(2)

        'Check Date Validity

        If (Day < 1 Or Day > 31) Then
            cstmvdtxtDate.ErrorMessage = "Day must be between 1 and 31"
            args.IsValid = False
        End If

        If (Month < 1 Or Month > 12) Then
            cstmvdtxtDate.ErrorMessage = "Month must be between 1 and 12"
            args.IsValid = False
        End If

        If ((Month = 4 Or Month = 6 Or Month = 9 Or Month = 11) And Day = 31) Then
            cstmvdtxtDate.ErrorMessage = "Month " & Month & "doesn't have 31 days!"
            args.IsValid = False
        End If

        If (Month = 2) Then

            'Check for February 29th
            IsLeap = (Year Mod 4 = 0 And (Year Mod 100 <> 0 Or Year Mod 400 = 0))

            If (Day > 29 Or (Day = 29 And IsLeap = False)) Then
                cstmvdtxtDate.ErrorMessage = "February " & Year & " doesn't have " & Day & " days!"
                args.IsValid = False

            End If


        End If

    End Sub

    Public ReadOnly Property DateValue() As String
        Get
            Return txtDate.Text
        End Get

    End Property
End Class
